<?php

namespace App\Http\Controllers\Frontend;

use DB;
use Log;
use Auth;
use Hash;
use Mail;
use Cache;
use Storage;
use Carbon\Carbon;
use App\Models\Tag;
use App\Models\User;
use App\Models\Post;
use App\Models\Sort;
use App\Models\Role;
use App\Models\Like;
use App\Models\Testc;
use App\Models\Comment;
use App\Models\Country;
use App\Jobs\BlogIndexData;
use Illuminate\Http\Request;
use App\Jobs\SendReminderEmail;
use App\Events\PodcastWasPurchased;
use App\DesignPatterns\ObserverTest;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Redis;
use Illuminate\Broadcasting\BroadcastManager;

class BlogController extends Controller
{
    public function __construct()
    {
        // 基于 HTTP 的认证
        // $this->middleware('auth.basic');
    }

    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        // $test->testNotify();die;
        $user = User::findOrFail(1);  //查看user表中ID为1的记录是否存在，存在返回用户实例，否则提示not found
        $tag = $request->get('tag');  //从get、post请求和自定义的参数中查询，存在返回参数值，否则null
        $sort_id = $request->get('sort_id');
        $data = $this->dispatch(new BlogIndexData($tag, $sort_id));  //将取帖子数据的任务分发到队列中执行
        // $data = [];
        $sorts = Sort::all();
        $layout = $tag ? Tag::layout($tag)->first() : 'frontend.blog.index';

        return view($layout, $data)->with(compact('user', 'sorts'));
    }

    /**
     * Display the specified resource.
     *
     * @param Model $slug
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function showPost($slug, Request $request)
    {
        $user = User::findOrFail(1);
        $post = Post::with('tags')->whereSlug($slug)->firstOrFail();
        $tag = $request->get('tag');
        $title = $post->title;
        if ($tag) {
            $tag = Tag::whereTag($tag)->firstOrFail();
        }

        return view($post->layout, compact('post', 'tag', 'slug', 'title', 'user'));
    }
}
